<template>
  <div>
    <!-- 标签或分类名 -->
    <div :class="categoryOrTag + ' banner'">
      <h1 class="banner-title animated fadeInDown">{{ title }} - {{ name }}</h1>
    </div>
    <div class="article-list-wrapper">
      <v-row>
        <v-col md="4" cols="12" v-for="item of articleList" :key="item.id">
          <!-- 文章 -->
          <v-card class="animated zoomIn article-item-card">
            <div class="article-item-cover">
              <router-link :to="`/articles/list/${item.tags._id}`">
                <!-- 缩略图 -->
                <v-img
                  class="on-hover"
                  width="100%"
                  height="100%"
                  :src="item.tags.icon"
                />
              </router-link>
            </div>
            <div class="article-item-info">
              <!-- 文章标题 -->
              <div>
                <router-link :to="`/articles/list/${item.tags._id}`">
                  {{ item.tags.title }}
                </router-link>
              </div>
              <div style="margin-top: 0.375rem">
                <!-- 发表时间 -->
                <v-icon size="20">mdi-clock-outline</v-icon>
                {{ item.tags.createdAt | date }}
                <!-- 文章分类 -->
                <router-link
                  :to="`/categories/${item.tags.categories}`"
                  class="float-right"
                  v-for="it in cates"
                  :key="it._id"
                >
                  <div v-if="it._id == item.tags.categories">
                    <v-icon>mdi-bookmark</v-icon>{{ it.name }}
                  </div>
                </router-link>
              </div>
            </div>
            <!-- 分割线 -->
            <v-divider></v-divider>
            <!-- 文章标签 -->
            <div class="tag-wrapper">
              <router-link
                :to="`/tags/${tag}`"
                class="tag-btn"
                v-for="tag in item.tags.tags"
                :key="tag"
              >
                <div v-for="it in tags" :key="it._id">
                  <div v-if="it._id == tag">
                    {{ it.name }}
                  </div>
                </div>
              </router-link>
            </div>
          </v-card>
        </v-col>
      </v-row>
      <!-- 无限加载 -->
      <infinite-loading @infinite="infiniteHandler">
        <div slot="no-more" />
      </infinite-loading>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    id: { required: true },
  },
  created() {
    this.getRequest();
    const path = this.$route.path;
    if (path.indexOf("/categories") != -1) {
      this.title = "分类";
      this.categoryOrTag = "category-banner";
    } else {
      this.title = "标签";
      this.categoryOrTag = "tag-banner";
    }
  },
  data: function () {
    return {
      current: 1,
      articleList: [],
      name: "",
      title: "",
      categoryOrTag: "",
      cates: {},
      tags: {},
    };
  },
  methods: {
    async infiniteHandler($state) {
      const res = await this.$http.get(`tags`);
      let newArray = [];
      const id = this.id;
      res.data.forEach(v => {
        for (let i = 0; i < v.list.length; i++) {
          if (v.list[i]._id === id) {
            newArray.push(v);
          }
        }
      });
      this.current++;
      this.name = newArray[0]._id;
      this.articleList.push(...newArray[0].list);
      $state.loaded();
      $state.complete();
    },
    async getRequest() {
      const resCate = await this.$http.get(`cates/list`);
      const resTap = await this.$http.get(`tags/list`);
      this.cates = resCate.data;
      this.tags = resTap.data;
    },
  },
};
</script>

<style scoped>
.tag-banner {
  background: url(https://2u-blog.oss-cn-beijing.aliyuncs.com/%E7%BC%98%E7%BB%93%E7%A5%9E2.jpg)
    center center / cover no-repeat;
  background-color: #49b1f5;
  margin-bottom: 100px;
}
.category-banner {
  background: url(https://2u-blog.oss-cn-beijing.aliyuncs.com/%E7%BC%98%E7%BB%93%E7%A5%9E2.jpg)
    center center / cover no-repeat;
  background-color: #49b1f5;
}
@media (min-width: 760px) {
  .article-list-wrapper {
    max-width: 1106px;
    margin: 370px auto 1rem auto;
  }
  .article-item-card:hover {
    transition: all 0.3s;
    box-shadow: 0 4px 12px 12px rgba(7, 17, 27, 0.15);
  }
  .article-item-card:not(:hover) {
    transition: all 0.3s;
  }
  .article-item-card:hover .on-hover {
    transition: all 0.6s;
    transform: scale(1.1);
  }
  .article-item-card:not(:hover) .on-hover {
    transition: all 0.6s;
  }
  .article-item-info {
    line-height: 1.7;
    padding: 15px 15px 12px 18px;
    font-size: 15px;
  }
}
@media (max-width: 759px) {
  .article-list-wrapper {
    margin-top: 230px;
    padding: 0 12px;
  }
  .article-item-info {
    line-height: 1.7;
    padding: 15px 15px 12px 18px;
  }
}
.article-item-card {
  border-radius: 8px !important;
  box-shadow: 0 4px 8px 6px rgba(7, 17, 27, 0.06);
}
.article-item-card a {
  transition: all 0.3s;
}
.article-item-cover {
  height: 220px;
  overflow: hidden;
}
.article-item-card a:hover {
  color: #8e8cd8;
}
.tag-wrapper {
  padding: 10px 15px 10px 18px;
}
.tag-wrapper a {
  color: #fff !important;
}
.tag-btn {
  display: inline-block;
  font-size: 0.725rem;
  line-height: 22px;
  height: 22px;
  border-radius: 10px;
  padding: 0 12px !important;
  background: linear-gradient(to right, #bf4643 0%, #6c9d8f 100%);
  opacity: 0.6;
  margin-right: 0.5rem;
}
</style>
